<?php
namespace App\Http\Middleware\Auth;

use Closure;
use App\Core\Entity\Auth\UserEntity;

/**
 * 用户登录检查
 */
class User
{

    /**
     * 用户实体
     *
     * @var \App\Core\Entity\Auth\UserEntity
     */
    protected $userEntity;

    /**
     * 初始化用户实体
     *
     * @param UserEntity $userEntity            
     */
    public function __construct(UserEntity $userEntity)
    {
        $this->userEntity = $userEntity;
    }

    /**
     * 前置 | 后置
     *
     * @param \Illuminate\Http\Request $request            
     * @param \Closure $next            
     * @param string $userType            
     * @return mixed
     */
    public function handle($request, Closure $next, $userType)
    {
        if (! $this->userEntity->isValidAuthToken() || ! $this->userEntity->isValid()) {
            abort(401);
        }
        
        if ($this->userEntity->getUserType() != $userType) {
            abort(403);
        }
        
        return $next($request);
    }
}
